Hands-On Exercise 2

Hands-On exercise to practice ggiraph, plotlyr and gganimate

Tan Jit Kai https://www.linkedin.com/in/jit-kai-tan-6b2aba12a/ (Singapore Management University Master of IT in Business)https://scis.smu.edu.sg/master-it-business
2022-01-29
Installing and running the packages
packages = c('tidyverse','readxl','ggiraph','plotly','gganimate','DT','patchwork','gifski','gapminder')

for(p in packages){library
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p,character.only = T)
}

Loading the CSV file

exam_data <- read_csv("data/Exam_data.csv")
plot
p <- ggplot(data = exam_data,
       aes(x = MATHS)) +
  geom_dotplot_interactive(
    aes(tooltip = ID) ,
    stackgroups = TRUE,
    binwidth = 1,
    method = "histodot") +
  scale_y_continuous(NULL,
                     breaks = NULL)
girafe(
  ggobj = p,
  width_svg = 6,
  height_svg = 6*0.618
)                     

plot with class dataid and patchwork for linking w eng graph

p1 <- ggplot(data = exam_data,
       aes(x = MATHS)) +
  geom_dotplot_interactive(
    aes(data_id = CLASS,
        tooltip = ID),
    stackgroups = TRUE,
    binwidth = 1,
    method = "histodot") +
  coord_cartesian(xlim=c(0,100)) +
  scale_y_continuous(NULL,
                     breaks = NULL)
girafe(
  ggobj = p,
  width_svg = 6,
  height_svg = 6*0.618,
  options = list(
    opts_hover(css = "fill: #202020;"),
    opts_hover_inv(css = "opacity: 0.2;")
  )
)                     
english graph
p2 <- ggplot(data = exam_data,
       aes(x = ENGLISH)) +
  geom_dotplot_interactive(
    aes(data_id = CLASS,
        tooltip = ID),
    stackgroups = TRUE,
    binwidth = 1,
    method = "histodot") +
  coord_cartesian(xlim=c(0,100)) +
  scale_y_continuous(NULL,
                     breaks = NULL)
girafe(code = print(p1 / p2),
  width_svg = 6,
  height_svg = 6*0.618,
  options = list(
    opts_hover(css = "fill: #202020;"),
    opts_hover_inv(css = "opacity: 0.2;")
  )
)                     
plotly scatter plot
plot_ly(data = exam_data,
        x = ~MATHS,
        y = ~ENGLISH,
        color = ~RACE,
        colors = "Set1")

creating a palette

pal <- c("pink","black","brown","green")

plot_ly(data = exam_data,
        x = ~MATHS,
        y = ~ENGLISH,
        text = ~paste("Student ID:", ID,
                      "<br>Class:", CLASS,
                      "<br>Race:", RACE),
        color = ~RACE,
        colors = pal) %>%
  layout(title = "English Score vs Math Score",
         xaxis = list(c(0,100)),
         yaxis = list(c(0,100)))

using ggplotly

d <- highlight_key(exam_data)
p1 <- ggplot(data = d,
             aes( x = MATHS, y = ENGLISH)) +
  geom_point(dotsize = 1) +
  coord_cartesian(xlim = c(0,100),
                  ylim = c(0,100))
p2 <- ggplot(data = d,
             aes(x = MATHS,y = SCIENCE)) +
  geom_point(dotsize = 1) +
  coord_cartesian(xlim = c(0,100),
                  ylim = c(0,100))
subplot(ggplotly(p1),
        ggplotly(p2))
interactive data table
DT::datatable(exam_data)
linked brushing cross talk
d <- highlight_key(exam_data)
p <- ggplot(data = exam_data,
             aes( x = MATHS, y = ENGLISH)) +
  geom_point(dotsize = 1) +
  coord_cartesian(xlim = c(0,100),
                  ylim = c(0,100))
gg <- highlight(ggplotly(p),
                "plotly_selected")
crosstalk::bscols(gg,
                  DT::datatable(d),
                  width = 5)
5